import execjs
import requests
import json
import csv

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36",
   }
session = requests.session()
session.headers = headers
url = "https://taxi.jtzyzg.org.cn/CZCJSY/restservices/http/single/query"

with open('taxi_des.js', encoding="utf-8") as f:
    exec = f.read()
js = execjs.compile(exec)

# 存储位csv
f = open('驾校信息.csv','a',encoding='utf8',newline='')
csv_writer = csv.writer(f)
csv_writer.writerow(['驾校地址','联系人电话','联系人','座位数','驾校名称'])

# 构建请求体
def return_data(pagenum):
    data = '{"bean":{"pagesize":8,"pagenum":"'+str(pagenum)+'","areaid":"-1"}}'
    return {
        "service": js.call('r', "LTPUCZC_getExamPlace"),
        "version": js.call('r',"1.0.0"),
        "data": js.call('r',data)
    }

for i in range(1,3):
    ret = session.post(url, data=json.dumps(return_data(i))).json()

    for item in ret.get('result').get('result').get('result'):
        address = item.get('address')
        linktel = item.get('linktel')
        linkperson = item.get('linkperson')
        seatnum = item.get('seatnum')
        examroomname = item.get('examroomname')
        csv_writer.writerow([address, linktel,linkperson, seatnum, examroomname])